import { xcxlogin } from '@/api/user'
import storage from '@/utils/storage'

export default function login() {
  return new Promise((resolve, reject) => {
    // 走登录了么
    wx.login({
      success: async res => {
        console.log(res.code )
        const { code, data } = await xcxlogin({ code: res.code })

        if (code === 206) {
          storage.setStorage('openId', data.openId)
          reject()
        }
        if (code === 200) {
          if (!data.temporaryFlag) {
            storage.setStorage('info', data.user)
          }
          storage.setStorage('openId', data.openId)
          storage.setStorage('regiterFlag', data.temporaryFlag)
          storage.setStorage('access_token', data.token)
          resolve()
        }
      },
      fail: (err) => {
        console.log(err)
      } 
    })
  })
}